<template>
  <div>
    <!-- <el-tabs tab-position="top" ref="tabs" style="height: 100%;">
      <el-tab-pane label="内部">
        <p v-for="(item, i) in nbList" :key="i" class="noticeDiv">
          <el-button type="text" class="latest-new-list__title" @click="noticeView(item)">{{
            item.name
          }}</el-button>
          <span class="latest-new-list__time">{{ item.createTime }}</span>
        </p>
      </el-tab-pane>
      <el-tab-pane label="学术">
        <p v-for="(item, i) in xsList" :key="i" class="noticeDiv">
          <el-button type="text" class="latest-new-list__title" @click="noticeView(item)">{{
            item.name
          }}</el-button>
          <span class="latest-new-list__time">{{ item.createTime }}</span>
        </p>
      </el-tab-pane>
      <el-tab-pane label="通知">
        <p v-for="(item, i) in tzList" :key="i" class="noticeDiv">
          <el-button type="text" class="latest-new-list__title" @click="noticeView(item)">{{
            item.name
          }}</el-button>
          <span class="latest-new-list__time">{{ item.createTime }}</span>
        </p>
      </el-tab-pane>
      <el-tab-pane label="招标">
        <p v-for="(item, i) in zbList" :key="i" class="noticeDiv">
          <el-button type="text" class="latest-new-list__title" @click="noticeView(item)">{{
            item.name
          }}</el-button>
          <span class="latest-new-list__time">{{ item.createTime }}</span>
        </p>
      </el-tab-pane>
    </el-tabs>

    <Systemcontent ref="systemcontent" v-if="isShow" :isShow="isShow" :record="noticeFile" @close="dialogClose">
    </Systemcontent> -->
    <el-form @submit.native.prevent ref="editForm">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item>
            <el-input v-model="input" prefix-icon="el-icon-search" placeholder="查询公告名称或公告类型">
            </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" @click="get()">查询 </el-button>
            <span v-if="user == '企业管理员'" style="margin-left: 10px;">
              <el-button type="primary" @click="add()">增加 </el-button>
              <el-button type="primary" @click="enable()">批量启用 </el-button>
              <el-button type="primary" @click="disable()">批量禁用 </el-button>
              <el-button type="primary" @click="deleteSelection">批量删除
              </el-button>
            </span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!--表格内容-->
    <el-table ref="list" :data="tableData" style="width: 100%; margin-bottom: 15px" border highlight-current-row
      :default-sort="{ prop: 'date', order: 'descending' }" @select-all="handleCheckedAllAndCheckedNone"
      @select="handleCheckedAllAndCheckedNone" @selection-change="handleSelectionChange"
      :header-cell-style="{ textAlign: 'center' }" :cell-style="{ textAlign: 'center' }">
      <el-table-column  v-if="user == '企业管理员'" fixed type="selection"> </el-table-column>
      <el-table-column  v-if="user == '企业管理员'" prop="id" label="ID"> </el-table-column>
      <el-table-column property="name" label="公告名称"> </el-table-column>
      <el-table-column property="content" label="公告内容" show-overflow-tooltip> </el-table-column>
      <el-table-column property="type" label="公告类型"> </el-table-column>
      <el-table-column  v-if="user == '企业管理员'" property="status" label="公告状态">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
            @change="statusChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column property="companyName" label="发布机构"></el-table-column>
      <el-table-column property="createTime" label="发布时间">
      </el-table-column>
      <el-table-column  v-if="user == '企业管理员'" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="rowEdit(scope.row)">编辑
          </el-button>
          <el-button type="danger" size="mini" @click="rowDel(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination :page-sizes="[10, 20, 30, 40, 100]" :page-size="pageSize" :total="total"
      @current-change="currentChange" @size-change="sizeChange" layout="total, sizes, prev, pager, next, jumper"
      style="padding-bottom: 15px">
    </el-pagination>

    <!-- 弹出层 -->
    <NoticeAdd ref="noticeAdd" v-if="addShow" :addShow="addShow" @dialogClose="dialogClose" @close="get()"></NoticeAdd>
    <NoticeEdit ref="noticeEdit" v-if="editShow" :editShow="editShow" :record="formFileds" @dialogClose="dialogClose"
      @close="get()"></NoticeEdit>
  </div>
</template>

<script>
// import { getNoticeByType } from "@/api/systemManage/noticeManage";
// import Systemcontent from "./systemcontent.vue";
import NoticeAdd from "@/view/systemManage/noticeManage/noticeAdd.vue";
import NoticeEdit from "@/view/systemManage/noticeManage/noticeEdit.vue";
import {
  findNoticeWithCompany,
  deleteNotice,
  deleteBatchNotice,
  updateNotice,
  updateBatchNotice,
} from "@/api/systemManage/noticeManage";
export default {
  name: "Table",
  components: {
    NoticeAdd,
    NoticeEdit
  },
  data () {
    return {
      // status: false,

      // nbList: [],
      // xsList: [],
      // tzList: [],
      // zbList: [],
      // isShow: false,
      // noticeFile: {},

      // formFileds: {},
      // // 内部公告
      // Internal: [],
      // // 学术活动
      // academic: [],
      // // 通知公告
      // inform: [],
      // // 招标公告信息
      // callBids: [],
      user:localStorage.getItem('role'),
      text: "",
      formFileds: {},
      multipleSelection: null,
      tableData: [],
      addShow: false,
      editShow: false,
      pageSize: 10,
      pageNum: 1,
      total: 0,
      input: "",
    };
  },

  methods: {
    handleCheckedAllAndCheckedNone (selection) {
      // 当前选中仅一行时操作-（当前表格行高亮）
      1 != selection.length && this.$refs.list.setCurrentRow();
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    dialogClose () {
      this.addShow = false;
      this.editShow = false;
      this.get();
    },
    rowEdit (row) {
      // //当前选中行
      this.formFileds = row;
      this.editShow = true;
    },
    rowDel (id) {
      // 让当前删除按钮失焦
      // event.target.blur();

      this.$confirm("确定要删除当前行吗？", "删除", {
        comfirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        deleteNotice("id=" + id).then((res) => {
          if (res) {
            this.$message.success("删除成功！");
            this.get();
          }
        });
      });
    },
    add () {
      this.addShow = true;
    },
    // 批量删除
    deleteSelection () {
      if (!this.multipleSelection) {
        this.$message.error("请至少选择一条数据进行删除！");
        return;
      } else {
        this.$confirm("数据将会永久删除，是否继续？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        }).then(() => {
          //生命空数组，这里的数组是后端需要的数据
          let array = [];
          //遍历勾选数据取自己需要的id等 这里是和后端需要的参数对应
          for (let i = 0; i < this.multipleSelection.length; i++) {
            let obj = this.multipleSelection[i].id;
            array.push(obj); //把取出来的对象push到数组里
          }
          let obj = { id: array };
          deleteBatchNotice(obj).then((res) => {
            //接口
            this.get(); //删除完成后初始化查询列表
          });
        });
      }
    },
    enable () {
      if (this.multipleSelection == "") {
        this.$message.warning("请至少选择一条数据进行编辑！");
      } else {
        let array = [];
        let array1 = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          let obj = this.multipleSelection[i].id;
          let obj1 = this.multipleSelection[i].status;
          obj1 = 0;
          array.push(obj);
          array1.push(obj1);
        }
        // let newdate = new Date();
        // let obj = { id: array, status: array1, createTime: newdate };
        let obj = { id: array, status: array1 };
        updateBatchNotice(obj).then((res) => {
          this.get(); //删除完成后初始化查询列表
        });
      }
    },
    disable () {
      if (this.multipleSelection == "") {
        this.$message.warning("请至少选择一条数据进行编辑！");
      } else {
        let array = [];
        let array1 = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          let obj = this.multipleSelection[i].id;
          let obj1 = this.multipleSelection[i].status;
          obj1 = 1;
          array.push(obj);
          array1.push(obj1);
        }
        let obj = { id: array, status: array1 };
        updateBatchNotice(obj).then((res) => {
          this.get(); //删除完成后初始化查询列表
        });
      }
    },
    get () {
      var par;
      par = {
        input: this.input,
        pageSize: this.pageSize,
        pageNum: this.pageNum,
      };

      findNoticeWithCompany(par).then((res) => {
        if (res) {
          this.tableData = res.data.list;
          this.total = res.data.total;
          for (let i = 0; i < this.tableData.length; i++) {
            if (this.tableData[i].status == 0) {
              this.tableData[i].status = true;
            } else {
              this.tableData[i].status = false;
            }
          }
          for (let i = 0; i < this.tableData.length; i++) {
            let newdate = this.tableData[i].createTime;
            this.tableData[i].createTime = newdate.substring(0, 10);
          }
        }
      });
    },
    currentChange (e) {
      this.pageNum = e;
      this.get();
    },
    sizeChange (e) {
      this.pageSize = e;
      this.get();
    },
    statusChange (row) {
      let obj = Object.assign({}, row);
      if (obj.status == true) {
        obj.status = 0;
      } else {
        obj.status = 1;
      }
      this.$delete(obj, "createTime");
      // let newdate = new Date();
      updateNotice(obj).then((res) => {
        this.get();
      });
    },
  },
  mounted () {
    this.get();
  },
  // components: { Systemcontent },
};
</script>

<style scoped lang="less">
/deep/ .el-tabs__item {
  width: 50%;
  text-align: center;
  font-size: 15px;
}

.el-tab-pane {
  background-color: #fff;
}

.noticeDiv {
  width: 50%;
  display: flex;
  justify-content: space-between;
  margin-left: 20px;
}

.latest-new-list__title {
  color: #666;
  font-size: 14px;
}

.latest-new-list__time {
  color: #666;
  font-size: 13px;
}
</style>
